class Block:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.color_text = (255,0,0)
        self.text_surface = None
        self.font = None
        self.text = ""
        self.image = None
        self.is_x_left = False
        self.is_y_donw = False

    def move(self, dx, dy):
        self.x += dx
        self.y += dy

    def setText(self,pygame,text):
        # 设置字体、大小
        self.text = text
        self.font = pygame.font.Font(None, 50)
        self.text_surface = self.font.render(text, True, self.color_text)
        # setattr(self,"text_surface",text_surface)
    def setImage(self,block_img):
        self.image = block_img

    def show(self):
        return (self.x,self.y,self.width,self.height)

    def size(self):
        return (self.width,self.height)

    def draw(self, screen,pygame):
        rect = pygame.Rect(self.show())
        if self.image is not None:
            screen.blit(self.image,(self.x,self.y))
        else:
            pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
        if self.text_surface is not None:
            self.text_surface = self.font.render(self.text, True, self.color_text)  # 重新渲染文本
            self.text_surface_rect = self.text_surface.get_rect()  # 获取新的矩形
            self.text_surface_rect.center = rect.center  # 更新中心点
            screen.blit(self.text_surface, self.text_surface_rect)  # 绘制文本



